#----------------------------------------------------------#
#  Democratic Resilience (Aurel Croissant and Lars Lott)   #
#----------------------------------------------------------#

# Title: "Democratic Resilience in the Twenty-First Century. Search for an Analytical Framework and Explorative Analysis" #
# Authors: "Croissant, Aurel and Lott, Lars", Heidelberg University and FAU Erlangen-Nürnberg
# date: 2025-05-07
# journal: Political Studies
# DOI: 10.1177/00323217251345779
# written under "R version 4.5.0 (2025-04-11 ucrt)"

#### Preliminaries ####

R.version$version.string

# clear workspace
rm(list=ls())

# load packages
library(here)
library(tidyverse)
library(magrittr)
library(reshape2)
library(ggpubr)
library(gapminder)
library(countrycode)
library(ggrepel)
library(ggthemes)
library(lubridate)
library(dotwhisker)


library(coda)
library(MASS)
library(rjags)
library(parallel)
library(magrittr)
library(dplyr)
library(tools)
library(devtools)

library(tinytable)
library(lavaan)
library(stargazer)
library(vutils)

################################################################################
################################################################################

#### Load Data for each single latent variable if possible ####

ds.basic.subset.2000 <- readRDS("data/ds.basic.subset.2000.rds")

###################################
## Macro-Institutional Dimension ##
###################################

## Fit model 1-dim model
dim1 <- cfa('f =~ democracy_stock + v2juhcind + v2juncind + v2juhccomp + v2jucomp +
            v2exrescon + v2lgotovst + v2lginvstp + v2lgqstexp',
            data=ds.basic.subset.2000, std.lv=TRUE, missing="ML")

## Loadings are Std.all column for f, Uniquenesses are Std.all columnv for Variances
## Reports TLI, RMSEA, and SRMR
summary(dim1, fit.measures=TRUE, standardized=TRUE)

## Extract Loadings and Uniquenesses ##

dim1_container <- lavInspect(dim1, "Std.all")

loadings_dim1 <- dim1_container$lambda
uniquenesses_dim1 <- diag(dim1_container$theta)


dim1_table  <- tibble(data.frame(Measure = c("democracy_stock", "v2juhcind","v2juncind", "v2juhccomp", "v2jucomp",
                                             "v2exrescon", "v2lgotovst", "v2lginvstp", "v2lgqstexp"), 
                                 Loadings = loadings_dim1, 
                                 Uniqueness = uniquenesses_dim1))
dim1_table$f <- round(dim1_table$f, 3)
dim1_table$Uniqueness <- round(dim1_table$Uniqueness, 3)

modelsummary::datasummary_df(dim1_table, output = "data/bfa/FFA_InstDim.docx", fmt = 3)

###################################
## Political Actors Dimension    ##
###################################

## Fit model 1-dim model
dim1 <- cfa('f =~ anti_pluralist_party_index_rev + v2cacamps_osp_rev + v2caviol_osp_rev',
            data=ds.basic.subset.2000, std.lv=TRUE, missing="ML")

## Loadings are Std.all column for f, Uniquenesses are Std.all columnv for Variances
## Reports TLI, RMSEA, and SRMR
summary(dim1, fit.measures=TRUE, standardized=TRUE)

## Extract Loadings and Uniquenesses ##

dim1_container <- lavInspect(dim1, "Std.all")

loadings_dim1 <- dim1_container$lambda
uniquenesses_dim1 <- diag(dim1_container$theta)


dim1_table  <- tibble(data.frame(Measure = c("anti_pluralist_party_index_rev", "v2cacamps_osp_rev", "v2caviol_osp_rev"), 
                                 Loadings = loadings_dim1, 
                                 Uniqueness = uniquenesses_dim1))
dim1_table$f <- round(dim1_table$f, 4)
dim1_table$Uniqueness <- round(dim1_table$Uniqueness, 4)

modelsummary::datasummary_df(dim1_table, output = "data/bfa/FFA_PolPar.docx",  fmt = 3)


###################################
## Civil Society Dimension       ##
###################################

## Fit model 1-dim model
dim1 <- cfa('f =~ v2pepwrgen + v2pepwrsoc + v2pepwrses + v2cseeorgs + v2csreprss + v2csprtcpt',
            data=ds.basic.subset.2000, std.lv=TRUE, missing="ML")

## Loadings are Std.all column for f, Uniquenesses are Std.all columnv for Variances
## Reports TLI, RMSEA, and SRMR
summary(dim1, fit.measures=TRUE, standardized=TRUE)

## Extract Loadings and Uniquenesses ##

dim1_container <- lavInspect(dim1, "Std.all")

loadings_dim1 <- dim1_container$lambda
uniquenesses_dim1 <- diag(dim1_container$theta)


dim1_table  <- tibble(data.frame(Measure = c("v2pepwrgen", "v2pepwrsoc", "v2pepwrses", "v2cseeorgs", "v2csreprss", "v2csprtcpt"), 
                                 Loadings = loadings_dim1, 
                                 Uniqueness = uniquenesses_dim1))
dim1_table$f <- round(dim1_table$f, 4)
dim1_table$Uniqueness <- round(dim1_table$Uniqueness, 4)

modelsummary::datasummary_df(dim1_table, output = "data/bfa/FFA_CivSocDim.docx",  fmt = 3)


###################################
## Political Community Dimension ##
###################################

## Fit model 1-dim model
dim1 <- cfa('f =~ parl + gov + police + leg + Satis',
            data=ds.basic.subset.2000, std.lv=TRUE, missing="ML")

## Loadings are Std.all column for f, Uniquenesses are Std.all columnv for Variances
## Reports TLI, RMSEA, and SRMR
summary(dim1, fit.measures=TRUE, standardized=TRUE)

## Extract Loadings and Uniquenesses ##

dim1_container <- lavInspect(dim1, "Std.all")

loadings_dim1 <- dim1_container$lambda
uniquenesses_dim1 <- diag(dim1_container$theta)


dim1_table  <- tibble(data.frame(Measure = c("parl", "gov", "police", "leg", "Satis"), 
                                 Loadings = loadings_dim1, 
                                 Uniqueness = uniquenesses_dim1))
dim1_table$f <- round(dim1_table$f, 4)
dim1_table$Uniqueness <- round(dim1_table$Uniqueness, 4)

modelsummary::datasummary_df(dim1_table, output = "data/bfa/FFA_PolComDim.docx",  fmt = 3)


